package com.cyt.work.site.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.site.dao.NoticeDao;
import com.cyt.work.site.model.po.Notice;
import com.cyt.work.site.model.vo.NoticeVo;
import com.cyt.work.site.service.NoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 微官网-校园资讯Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-8 15:17:40
 */
@Service("noticeService")
public class NoticeServiceImpl extends BaseServiceImpl<Notice> implements NoticeService {

    @Resource
    private NoticeDao noticeDao;
    @Resource
    private SysFileService sysFileService;

    @Autowired
    public void setBaseDao(NoticeDao noticeDao) {
        this.baseDao = noticeDao;
    }

    /**
     * 分页查询
     *
     * @param noticeVo 接受参数对象
     * @param pageNum  分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    public ReturnResult page(NoticeVo noticeVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<NoticeVo> list = noticeDao.selectByCondition(noticeVo);
        PageInfo<NoticeVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param noticeVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(NoticeVo noticeVo, SysUser sysUser) {
        if (StrUtil.isEmpty(noticeVo.getId())) {
            Notice notice = new Notice(sysUser);
            notice.setApplyId(noticeVo.getApplyId());
            notice.setTitle(noticeVo.getTitle());
            notice.setContent(noticeVo.getContent());
            notice.setNoticeType(noticeVo.getNoticeType());
            notice.setPhoto(noticeVo.getPhoto());
            notice.setPath(getFilePath(noticeVo.getPhoto()));
            notice.setReadNum(0);
            notice.setStatus(noticeVo.getStatus());
            int num = noticeDao.insert(notice);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            Notice notice = noticeDao.selectByPrimaryKey(noticeVo.getId());
            notice.setApplyId(noticeVo.getApplyId());
            notice.setTitle(noticeVo.getTitle());
            notice.setContent(noticeVo.getContent());
            notice.setNoticeType(noticeVo.getNoticeType());
            notice.setPhoto(noticeVo.getPhoto());
            notice.setPath(getFilePath(noticeVo.getPhoto()));
            notice.setStatus(noticeVo.getStatus());
            notice.setUpdateTime(new Date());
            notice.setUpdateUser(sysUser.getId());
            int num = noticeDao.updateByPrimaryKey(notice);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }

    /**
     * 根据条件逻辑删除
     *
     * @param noticeVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(NoticeVo noticeVo, SysUser sysUser) {
        //要修改的字段
        Notice notice = new Notice();
        notice.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        notice.setUpdateUser(sysUser.getId());
        notice.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(noticeVo.getId())) {
            criteria.andIn("id", Arrays.asList(noticeVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        noticeDao.updateByExampleSelective(notice, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param noticeVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(NoticeVo noticeVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(noticeVo.getId())) {
            criteria.andIn("id", Arrays.asList(noticeVo.getId().split(",")));
        }
        noticeDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 发布/取消发布
     *
     * @param noticeVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult updateStatus(NoticeVo noticeVo, SysUser sysUser) {
        //要修改的字段
        Notice notice = new Notice();
        notice.setStatus(noticeVo.getStatus());
        notice.setUpdateUser(sysUser.getId());
        notice.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(noticeVo.getId())) {
            criteria.andIn("id", Arrays.asList(noticeVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        noticeDao.updateByExampleSelective(notice, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * list查询
     *
     * @param noticeVo 参数接受对象
     * @return List<NoticeVo>
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    public List<NoticeVo> list(NoticeVo noticeVo) {
        return noticeDao.selectByCondition(noticeVo);
    }

    /**
     * 校园资讯分类查询
     *
     * @param noticeVo 参数接受对象
     * @return List<Map>
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    public List<Map> noticeCategory(NoticeVo noticeVo) {
        noticeVo.setStatus(String.valueOf(1));
        noticeVo.setRemoveTag(BaseConstant.REMOVE_TAG_NO);
        return noticeDao.noticeCategory(noticeVo);
    }

    /**
     * list查询
     *
     * @param noticeVo 接受参数对象
     * @return List<Notice>
     * @author rong yuan
     * @date 2019-08-21 11:22
     */
    @Override
    public List<Notice> listByExample(NoticeVo noticeVo) {
        Example example = new Example(Notice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(noticeVo.getApplyId())) {
            criteria.andEqualTo("applyId", noticeVo.getApplyId());
        }
        if (StringUtils.isNotBlank(noticeVo.getNoticeType())) {
            criteria.andEqualTo("noticeType", noticeVo.getNoticeType());
        }
        if (StringUtils.isNotBlank(noticeVo.getStatus())) {
            criteria.andEqualTo("status", noticeVo.getStatus());
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        return noticeDao.selectByExample(example);
    }

    /**
     * 校园资讯统计
     *
     * @param noticeVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 15:17:40
     */
    @Override
    public ReturnResult statisticsByType(NoticeVo noticeVo) {
        noticeVo.setStatus(String.valueOf(1));

        List<Integer> result = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            noticeVo.setNoticeType(String.valueOf(i));
            List<Notice> noticeList = this.listByExample(noticeVo);
            result.add(noticeList == null ? 0 : noticeList.size());
        }
        return new ReturnResult(result);
    }
}